//
//  LSWaveView.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2018/12/18.
//  Copyright © 2018 wpkj. All rights reserved.
//

#import "LSWaveView.h"

@interface LSWaveView ()

@property (nonatomic,strong)CAShapeLayer *firstWavesLayer;
@property (nonatomic,strong)UIColor *waveColor;

@property (nonatomic, assign) CGFloat waveA;
@property (nonatomic, assign) CGFloat waveW;
@property (nonatomic, assign) CGFloat offsetX;
@property (nonatomic, assign) CGFloat currentK;
@property (nonatomic, assign) CGFloat wavesSpeed;
@property (nonatomic, assign) CGFloat wavesWidth;
@end

@implementation LSWaveView

/*
 y =Asin（ωx+φ）+C
 A表示振幅，也就是使用这个变量来调整波浪的高度
 ω表示周期，也就是使用这个变量来调整在屏幕内显示的波浪的数量
 φ表示波浪横向的偏移，也就是使用这个变量来调整波浪的流动
 C表示波浪纵向的位置，也就是使用这个变量来调整波浪在屏幕中竖直的位置。
 */

- (instancetype)initWithFrame:(CGRect)frame withWaveColor:(UIColor *)waveColor andWaveA:(CGFloat)waveA andWaveW:(CGFloat)waveW andOffsetX:(CGFloat)offsetX andCurrentK:(CGFloat)currentK andWavesSpeed:(CGFloat)wavesSpeed andWavesWidth:(CGFloat)wavesWidth {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.layer.masksToBounds = YES;
        self.waveColor = waveColor;
        self.waveA = waveA;
        self.waveW = waveW;
        self.offsetX = offsetX;
        self.currentK = currentK;
        self.wavesSpeed = wavesSpeed;
        self.wavesWidth = wavesWidth;
        [self setUpWaves];
    }
    return self;
}


- (void)setUpWaves{
    
    //初始化layer
    if (self.firstWavesLayer == nil) {
        
        //初始化
        self.firstWavesLayer = [CAShapeLayer layer];
        //设置闭环的颜色
        self.firstWavesLayer.fillColor = self.waveColor.CGColor;
        //设置边缘线的颜色
        //_firstWaveLayer.strokeColor = [UIColor blueColor].CGColor;
        //设置边缘线的宽度
        //self.firstWavesLayer.lineWidth = 1.0;
        //        self.firstWavesLayer.strokeStart = 0.0;
        //        self.firstWavesLayer.strokeEnd = 0.8;
        
        [self.layer addSublayer:self.firstWavesLayer];
    }
    
    //启动定时器
    _wavesDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(getCurrentWave:)];
    
    [_wavesDisplayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}

-(void)getCurrentWave:(CADisplayLink *)displayLink{
    //实时的位移
    self.offsetX += self.wavesSpeed;
    [self setCurrentFirstWaveLayerPath];
}

-(void)setCurrentFirstWaveLayerPath{
    
    //创建一个路径
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGFloat y = self.currentK;
    //将点移动到 x=0,y=currentK的位置
    CGPathMoveToPoint(path, nil, 0, y);
    
    for (NSInteger i =0.0f; i<=self.wavesWidth; i++) {
        //正弦函数波浪公式
        y = self.waveA * sin(self.waveW * i+ self.offsetX)+self.currentK;
        
        //将点连成线
        CGPathAddLineToPoint(path, nil, i, y);
    }
    
    CGPathAddLineToPoint(path, nil, self.wavesWidth, self.frame.size.height);
    CGPathAddLineToPoint(path, nil, 0, self.frame.size.height);
    
    CGPathCloseSubpath(path);
    self.firstWavesLayer.path = path;
    
    //使用layer 而没用CurrentContext
    CGPathRelease(path);
}

-(void)dealloc {
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
